package com.woniu.smart.service.impl;

import com.woniu.smart.dto.ContractDTO;
import com.woniu.smart.dto.ContractInformationDTO;
import com.woniu.smart.entity.Contract;
import com.woniu.smart.entity.Enterprise;
import com.woniu.smart.entity.Room;
import com.woniu.smart.mapper.*;
import com.woniu.smart.service.ContractService;
import com.woniu.smart.vo.ContractVo;
import com.woniu.smart.vo.ContractsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 对合同操作实现方法
 * @Author: MeiBing
 * @Date: 2022/10/20 14:00
 */
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private InformationMapper informationMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private StaffsMapper staffsMapper;



    @Override
    public int deleteByPrimaryKey(Long id) {
        return contractMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteAll(List delList) {
        return contractMapper.deleteByList(delList);
    }

    ;


    @Override
    public int insert(Contract record) {
        return 0;
    }

    /**
     * 根据传入合同对象
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(Contract record) {

        return contractMapper.insertSelective(record);

    }

    ;


    /**
     * 根据页面信息提取
     * 新增合同
     * 新增企业
     * 新增企业人员表
     * 新增账单
     * 修改房间租售状态
     *
     * @param contractInformationDTO
     * @return
     */
    @Transactional
    @Override
    public int insertSelectives(ContractInformationDTO contractInformationDTO) {

        int i = roomMapper.selectAllRoomNameAndBuildingName(contractInformationDTO);

        //1、根据页面信息新增合同表。并获得合同id
        contractInformationDTO.setContractAtt("新签");
        contractInformationDTO.setContractState("生效中");
        contractInformationDTO.setRoomId(i);
        contractMapper.insertSelectives(contractInformationDTO);
        //2、插入账单信息，并获得账单id
        informationMapper.insertSelectiveForContractInformationDTO((contractInformationDTO));

        //3、新增企业信息
        enterpriseMapper.insertSelectiveByContract(contractInformationDTO);
        //4、新增企业人员表信息
        contractInformationDTO.setStaffstypeId(1);
        staffsMapper.insertSelectivesByContract(contractInformationDTO);

        //5、更新房间的待租状态
        Room room = new Room();
        room.setRoomId(i);
        room.setRoomStatus(2);
        room.setEnterpriseId(contractInformationDTO.getEnterpriseId());
        roomMapper.updateByPrimaryKeySelective(room);
        //6、更新企业表里的负责人id
        Enterprise enterprise = new Enterprise();
        enterprise.setEnterpriseId(contractInformationDTO.getEnterpriseId());//企业id
        enterprise.setStaffsId(contractInformationDTO.getStaffsId());//负责人id
        enterpriseMapper.updateByPrimaryKeySelective(enterprise);
        //7、更新合同表里的企业id，负责人id，负责人姓名，电话
        Contract contract = new Contract();
        contract.setContractId(contractInformationDTO.getContractId());//合同id
        contract.setEnterpriseId(contractInformationDTO.getEnterpriseId());//企业id
        contract.setStaffsId(contractInformationDTO.getStaffsId());//企业负责人id
        contract.setInformationId(contractInformationDTO.getInformationId());//账单id
        int r = contractMapper.updateByPrimaryKeySelective(contract);
        return r;
    }

    ;

    /**
     * 查询合同根据id
     *
     * @param id
     * @return
     */
    @Override
    public Contract selectByPrimaryKey(Long id) {

        return contractMapper.selectByPrimaryKey(id);

    }


    @Override
    public List<Contract> selectContractAll() {
        return contractMapper.selectContractAll();
    }

    @Override
    public List<ContractsVO> selectContractAllS(ContractDTO contractDTO) {
        List<ContractsVO> contractList = contractMapper.selectContractAllS(contractDTO);
        return contractList;
    }

    /**
     * 根据定义的合同模板对象获取筛选
     *
     * @param contractvo
     * @return
     */
    @Override
    public List<Contract> queryContractByContractVo(ContractVo contractvo) {
        return contractMapper.queryContractByContractVo(contractvo);
    }

    ;

    @Override
    public int updateByPrimaryKeySelective(Contract record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Contract record) {
        return 0;
    }
}
